package com.gxa.service.impl.system;

import com.gxa.mapper.system.LoginMapper;
import com.gxa.mapper.system.MenuMapper;
import com.gxa.mapper.system.UserMapper;
import com.gxa.pojo.entity.system.Menu;
import com.gxa.pojo.entity.system.User;
import com.gxa.service.system.LoginService;
import com.gxa.utils.AddressTreeUtils;
import com.gxa.utils.JwtUtils;
import com.gxa.utils.Md5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lch
 * @Date 2024/2/22 14:26
 * @Description
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private LoginMapper loginMapper;

    @Resource
    private MenuMapper menuMapper;

    public static List<Menu> menuList;

    /**
     * 用户登录
     *
     * @param jobNumber 员工工号
     * @param password  密码
     * @return
     */
    @Override
    public String login(String jobNumber, String password) {
        //先校验用户输入是否满足条件，正则表达式==============================

        User user = loginMapper.queryUserByJobNumber(jobNumber);
        if (user != null) {
            //使用数据库查询的盐将用户输入密码加密
            String tempPassword = Md5Utils.md5Password(password, user.getSalt());
            System.out.println(password);
            System.out.println(tempPassword);
            if (tempPassword.equals(user.getPassword())) {
                //查询出用户的左侧菜单栏
                menuList = AddressTreeUtils.buildTree(menuMapper.queryDynamicMenu(user.getId()));
                //登录成功，返回一个token
                return JwtUtils.token(user.getId(), user.getJobNumber(), user.getUserName());
            }
        }
        return null;
    }
}
